﻿#region  
//=====================================================
// 文件名: Test_LoadABPackage                
// 创建者: #AuthorName#                
// 创建时间: #CreateTime#              	
// Email: #AuthorEmail#               
// 描述: 当前脚本的功能              
// 修改者列表: 修改者名字以及修改功能      
// 版权所有: #Copyright#                 
//======================================================
#endregion 



using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using System.Xml.Serialization;
using FrameWork.AB;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Test_LoadABPackage : MonoBehaviour
{

    private void Start()
    {
        TestLoadAB();
        Destroy(this.gameObject);
    }

    void TestLoadAB()
    {

        AssetBundle assetBundle1 = AssetBundle.LoadFromFile(Application.streamingAssetsPath+ "/config");
        TextAsset textAsset1 = assetBundle1.LoadAsset<TextAsset>("AssetBundleConfig.bytes");
        MemoryStream stream = new MemoryStream(textAsset1.bytes);
        BinaryFormatter bf = new BinaryFormatter();
        AssetBundleConfig ABSerilize = (AssetBundleConfig)bf.Deserialize(stream);
        stream.Close();
        string path = "Assets/GameMain/Picture/timg.jpg";
        uint crc = CRC32.GetCRC32(path);
        ABBase abBase = null;
        for (int i = 0; i < ABSerilize.ABList.Count; i++)
        {
            if (ABSerilize.ABList[i].Crc == crc)
            {
                abBase = ABSerilize.ABList[i];
                break;
            }
        }

        //for (int i = 0; i < abBase.ABDependce.Count; i++)
        //{
        //    AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependce[i]);
        //}
        //Debug.Log(Application.streamingAssetsPath + "/" + abBase.ABPackageName);
        //AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABPackageName);
        //GameObject go = GameObject.Instantiate(assetBundle.LoadAsset<GameObject>(abBase.AssetName));

        Image go2 = GameObject.Find("Image").GetComponent<Image>();
        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABPackageName);
        go2.sprite =  assetBundle.LoadAsset<Sprite>("timg");
    }

}
